package com.xl.yygh.hosp.controller.api;

import com.xl.yygh.common.exception.YyghException;
import com.xl.yygh.common.helper.HttpRequestHelper;
import com.xl.yygh.common.result.Result;
import com.xl.yygh.common.result.ResultCodeEnum;
import com.xl.yygh.common.utils.MD5;
import com.xl.yygh.hosp.service.DepartmentService;
import com.xl.yygh.hosp.service.HospitalService;
import com.xl.yygh.hosp.service.HospitalSetService;
import com.xl.yygh.hosp.service.ScheduleService;
import com.xl.yygh.model.model.hosp.Department;
import com.xl.yygh.model.model.hosp.Hospital;
import com.xl.yygh.model.model.hosp.Schedule;
import com.xl.yygh.model.vo.hosp.DepartmentQueryVo;
import com.xl.yygh.model.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author XiangLiang
 * @date 2023/10/24 14:14
 * @description 医院管理远程调用API接口
 */
@RestController
@Api(tags = "医院管理API接口")
@RequestMapping("/api/hosp")
public class ApiController {
    @Resource
    private HospitalService hospitalService;

    @Resource
    private HospitalSetService hospitalSetService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;

    /**
     * 医院签名验证
     * @param hoscode 医院编号
     * @param hosSign 第三方系统医院签名
     */
    public void verifySign(String hoscode, String hosSign) {
        // 通过其它系统传递过来的医院编码获取本系统的该医院签名
        String localSign = hospitalSetService.getSignByHoscode(hoscode);
        // 本系统签名加密
        String localSignEncrypt = MD5.encrypt(localSign);
        // 判断签名是否一致
        if (!localSignEncrypt.equals(hosSign)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }

    @ApiOperation(value = "上传医院信息")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest httpServletRequest) {
        // 通过Http请求获取ParameterMap并转为Map
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编码和签名，签名是加密过的
        String hoscode = (String) map.get("hoscode");
        String hosSign = (String) map.get("sign");

        /* 通过医院签名验证 */
        this.verifySign(hoscode, hosSign);
        // 第三方系统中logoData是把图片转为Base64，会把 “+” 转为 “ ”，需要转回来，不然无法显示图片
        String logoData = (String) map.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        map.put("logoData", logoData);

        // 验证通过才保存到MongoDB
        hospitalService.saveHospital(map);

        return Result.ok();
    }

    @ApiOperation(value = "查询医院信息")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest httpServletRequest) {
        // 通过Http请求获取ParameterMap并转为Map
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编码和签名，签名是加密过的
        String hoscode = (String) map.get("hoscode");
        String hosSign = (String) map.get("sign");

        /* 通过医院签名验证 */
        this.verifySign(hoscode, hosSign);

        // 验证通过才根据签名传回医院信息
        Hospital hospital = hospitalService.getHospital(hoscode);

        return Result.ok(hospital);
    }

    @ApiOperation(value = "上传科室信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest httpServletRequest) {
        // 通过Http请求获取ParameterMap并转为Map
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编码和签名，签名是加密过的
        String hoscode = (String) map.get("hoscode");
        String hosSign = (String) map.get("sign");

        /* 通过医院签名验证 */
        this.verifySign(hoscode, hosSign);

        // 验证通过才保存到MongoDB
        departmentService.saveDepartment(map);

        return Result.ok();
    }

    @ApiOperation(value = "查询科室信息")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest httpServletRequest) {
        // 通过Http请求获取ParameterMap并转为Map
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编码和签名，签名是加密过的
        String hoscode = (String) map.get("hoscode");
        String hosSign = (String) map.get("sign");

        /* 通过医院签名验证 */
        this.verifySign(hoscode, hosSign);

        // 验证通过才根据签名传回科室信息
        String depcode = (String) map.get("depcode");
        Integer page = Integer.parseInt((String) map.getOrDefault("page", 1));
        Integer limit = Integer.parseInt((String) map.getOrDefault("limit", 10));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);

        // 科室分页查询
        Page<Department> pageList = departmentService.selectPage(page, limit, departmentQueryVo);

        return Result.ok(pageList);
    }

    @ApiOperation(value = "删除科室信息")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest httpServletRequest) {
        // 通过Http请求获取ParameterMap并转为Map
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编码和签名，签名是加密过的
        String hoscode = (String) map.get("hoscode");
        String hosSign = (String) map.get("sign");
        String depcode = (String) map.get("depcode");

        /* 通过医院签名验证 */
        this.verifySign(hoscode, hosSign);

        // 科室删除信息
        departmentService.removeDepartment(hoscode, depcode);

        return Result.ok();
    }

    @ApiOperation(value = "上传排班信息")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest httpServletRequest) {
        // 通过Http请求获取ParameterMap并转为Map
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编码和签名，签名是加密过的
        String hoscode = (String) map.get("hoscode");
        String hosSign = (String) map.get("sign");

        /* 通过医院签名验证 */
        this.verifySign(hoscode, hosSign);

        // 验证通过才保存到MongoDB
        scheduleService.saveSchedule(map);

        return Result.ok();
    }

    @ApiOperation(value = "查询排班信息")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest httpServletRequest) {
        // 通过Http请求获取ParameterMap并转为Map
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编码和签名，签名是加密过的
        String hoscode = (String) map.get("hoscode");
        String hosSign = (String) map.get("sign");

        /* 通过医院签名验证 */
        this.verifySign(hoscode, hosSign);

        // 验证通过才根据签名传回科室信息
        String depcode = (String) map.get("depcode");
        Integer page = Integer.parseInt((String) map.getOrDefault("page", 1));
        Integer limit = Integer.parseInt((String) map.getOrDefault("limit", 10));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        // 排班分页查询
        Page<Schedule> pageList = scheduleService.findSchedule(page, limit, scheduleQueryVo);

        return Result.ok(pageList);
    }

    @ApiOperation(value = "删除排班信息")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest httpServletRequest) {
        // 通过Http请求获取ParameterMap并转为Map
        Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        // 获取医院编码和签名，签名是加密过的
        String hoscode = (String) map.get("hoscode");
        String hosSign = (String) map.get("sign");
        String hosScheduleId = (String) map.get("hosScheduleId");

        /* 通过医院签名验证 */
        this.verifySign(hoscode, hosSign);

        // 删除排班信息
        scheduleService.removeSchedule(hoscode, hosScheduleId);

        return Result.ok();
    }

}
